import { GCellRenderer } from "../../../../com/mui/controls/GCellRenderer";
import { PetListItemInfo } from "./info/PetListItemInfo";
import { PetUtil } from "./info/PetUtil";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { Language } from "../../../../extend/language/Language";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { EPetStatus } from "../../../../Message/Game/EPetStatus";
import { Cache } from "../../cache/Cache";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { PetItem } from "./PetItem";
import { ResouceConst } from "../../../component/gconst/ResouceConst";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { KeyBoardManager } from "../../../../com/gengine/keyBoard/KeyBoardManager";
import { SPetInfo } from "../../../../Message/Game/SPetInfo";
import { EffectManager } from "../../manager/EffectManager";
import { ScaleBitmap } from "../../../../com/mui/display/ScaleBitmap";
import { LineBox } from "../guide/LineBox";
//class PetListRenderer
    
    export  class PetListRenderer extends GCellRenderer
    {
        constructor()
        {
            super();
            this.init();
            addEventListener(flash.events.MouseEvent.ROLL_OVER, this.onMouseOver);
            addEventListener(flash.events.MouseEvent.ROLL_OUT, this.onMouseOut);
            this.buttonMode = true;
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            super.data = arg1;
            this._petItemInfo = arg1 as PetListItemInfo;
            if (this._petItemInfo) 
            {
                if (this._petItemInfo.petInfo) 
                {
                    this._name = PetUtil.getPetName(this._petItemInfo.petInfo, true);
                    if (PetUtil.isBattled(this._petItemInfo.petInfo)) 
                    {
                        this._nameTxt.htmlText = "<textFormat leading=\'5\'>" + this._name + "\n" + HTMLUtil.addColor(Language.getString(54284), GlobalStyle.color6) + "</textFormat>";
                        this._nameTxt.y = 3;
                    }
                    else if (this._petItemInfo.petInfo.status != EPetStatus._EpetTrain) 
                    {
                        this._nameTxt.htmlText = this._name;
                        this._nameTxt.y = 12;
                    }
                    else 
                    {
                        this._nameTxt.htmlText = "<textFormat leading=\'5\'>" + this._name + "\n" + HTMLUtil.addColor(Language.getString(54285), GlobalStyle.color6) + "</textFormat>";
                        this._nameTxt.y = 3;
                    }
                    this._petItem.dragSource = this._petItemInfo.petInfo;
                    if (Cache.instance.guide.guidePetMerge && Cache.instance.guide.guidePetMergeSelected) 
                    {
                        if (this._petItemInfo.petInfo.uid != Cache.instance.guide.guidePetMergetPetId) 
                        {
                            this.guideSelect(true);
                        }
                        else 
                        {
                            this.guideSelect(false);
                        }
                    }
                    else 
                    {
                        this.guideSelect(false);
                    }
                }
                else 
                {
                    this._nameTxt.text = "";
                    this._petItem.dragSource = null;
                    this.selected = false;
                }
                if (this._petItemInfo.isEnable) 
                {
                    this._enableTxt.text = "";
                }
                else 
                {
                    this._enableTxt.text = Language.getString(54286);
                }
            }
            if (this._petItemInfo.petInfo && this._petItemInfo.isEnable) 
            {
                this.mouseEnabled = true;
                this.mouseChildren = true;
            }
            else 
            {
                this.mouseEnabled = false;
                this.mouseChildren = false;
            }
            if (this._petItemInfo.isShowTip) 
            {
                this.setHirePet();
            }
            return;
        }

        /* internal  */init(): void
        {
            this._itemBg = ObjCreate.createBg(0, 0, 38, 38, this, ImagesConst.PackItemBg);
            this._enableTxt = ObjCreate.createTextField("", 5, 5, 35, 35, this);
            this._petItem = new PetItem();
            this._petItem.isDropAble = false;
            this._petItem.width = 32;
            this._petItem.height = 32;
            this._petItem.move(3, 3);
            this.addChild(this._petItem);
            this._hireTxt = ObjCreate.createTextField("", 0, 22, 38, 20, this, GlobalStyle.textFormat6);
            this._hireTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._nameTxt = ObjCreate.createTextField("", 42, 12, 220, 20, this);
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._nameTxt.multiline = true;
            this._nameTxt.mouseEnabled = false;
            this._overSkin = ResouceConst.getScaleBitmap(ImagesConst.Friend_over);
            this._overSkin.width = 50;
            this._overSkin.height = 43;
            this._overSkin.x = -20;
            this._overSkin.y = -3;
            this._selectedBg = GlobalClass.getBitmap(ImagesConst.SelectedBg);
            this._selectedBg.width = 110;
            this._selectedBg.height = 43;
            this._selectedBg.y = -3;
            this._selectedItem = GlobalClass.getBitmap(ImagesConst.SelectedItem);
            this._selectedItem.width = 38;
            this._selectedItem.height = 38;
            ObjCreate.createBg(0, 41, 95, 2, this, ImagesConst.SplitLine);
            return;
        }

        public /* override */ set selected(arg1: Boolean)
        {
            super.selected = arg1;
            if (KeyBoardManager.ctrlKey) 
            {
                return;
            }
            if (this._petItemInfo && this._petItemInfo.petInfo && this._petItemInfo.isEnable && arg1) 
            {
                addChildAt(this._selectedBg, 0);
                addChildAt(this._selectedItem, 1);
                this.dealSelected(this._petItemInfo.petInfo);
                if (this._itemBg.parent) 
                {
                    removeChild(this._itemBg);
                }
            }
            else 
            {
                if (this._selectedBg.parent) 
                {
                    removeChild(this._selectedBg);
                }
                if (this._selectedItem.parent) 
                {
                    removeChild(this._selectedItem);
                }
                if (this._itemBg.parent == null) 
                {
                    addChildAt(this._itemBg, 1);
                }
            }
            if (this._overSkin.parent) 
            {
                removeChild(this._overSkin);
            }
            return;
        }

        /* internal  */onMouseOver(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._petItemInfo && this._petItemInfo.petInfo && !this.selected) 
            {
                addChildAt(this._overSkin, 0);
            }
            return;
        }

        /* internal  */onMouseOut(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (!_selected) 
            {
                if (this._overSkin.parent) 
                {
                    removeChild(this._overSkin);
                }
            }
            return;
        }

        protected dealSelected(arg1: SPetInfo): void
        {
            return;
        }

        public get petInfo(): SPetInfo
        {
            return this._petItemInfo.petInfo;
        }

        public guideSelect(arg1: Boolean): void
        {
            if (arg1) 
            {
                if (!this._lineBox) 
                {
                    this._lineBox = EffectManager.getLineBox(100, 40, [16776960, 65280]);
                    this._lineBox.x = 0;
                    this._lineBox.y = 0;
                }
                if (!contains(this._lineBox)) 
                {
                    addChild(this._lineBox);
                }
            }
            else if (this._lineBox) 
            {
                if (contains(this._lineBox)) 
                {
                    removeChild(this._lineBox);
                }
                EffectManager.disposeLineBox(this._lineBox);
                this._lineBox = null;
            }
            return;
        }

        /* internal  */setHirePet(): void
        {
            var loc2=null;
            var loc1=this._petItemInfo.petInfo;
            if (loc1) 
            {
                loc2 = "";
                if (loc1.isBorrow) 
                {
                    loc2 = loc2 + Language.getString(54287);
                    loc2 = loc2 + (Language.getString(54288) + loc1.ownerName);
                    loc2 = loc2 + Language.getString(54289);
                    loc2 = loc2 + Language.getString(54290);
                    this._hireTxt.text = Language.getString(54291);
                }
                else 
                {
                    loc2 = loc2 + (Language.getString(54292) + "\n");
                    loc2 = loc2 + Language.getString(54626);
                    this._hireTxt.text = "";
                }
                loc2 = HTMLUtil.addColor(loc2, GlobalStyle.color2);
                this.toolTipData = "<textFormat leading=\'5\'>" + loc2 + "</textFormat>";
            }
            return;
        }

        /* internal */ /* var */_itemBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_noHoleBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_enableTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_petItem: PetItem;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_overSkin: ScaleBitmap;

        /* internal */ /* var */_selectedBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_selectedItem: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_hireTxt: Object /* flash.text.TextField */;

        protected /* var */_petItemInfo: PetListItemInfo;

        /* internal */ /* var */_name: string;

        /* internal */ /* var */_lineBox: LineBox;
    }
